<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userForm" :rules="rules" label-width="220px" :model="userInfo">
      <!--手机 -->

      <el-form-item label="手机" prop="mobile">
        <el-input v-model="userInfo.mobile" style="width:220px" />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <!-- <Uploading ref="Uploading" /> -->
        <Uploading v-model="userInfo.staffPhoto" />
        <!--  -->
        <!-- <img :src="userInfo.staffPhoto"> -->
      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="hSave">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { saveUserDetailById } from '@/api/employees'
import { getUserDetailById } from '@/api/user'
import { validMobile } from '@/utils/validate'
export default {
  data() {
    const validateMobile = (rule, value, callback) => {
      // 检查是否校验通过
      // /^1\d{10}$/
      if (!validMobile(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        mobile: [{ required: true, trigger: 'blur', validator: validateMobile, message: '手机号不能为空' }],
        timeOfEntry: [{ required: true, trigger: 'blur', message: '日期不能为空' }]
      },
      userId: this.$route.query.id,
      userInfo: {
        mobile: '',
        timeOfEntry: ''
      }
    }
  },
  created() {
    this.loadUserDetailById()
  },
  methods: {
    async loadUserDetailById() {
      try {
        const res = await getUserDetailById(this.$route.query.id)
        console.log('', res)
        // 保存数据
        this.userInfo = res.data
        // 当前用户的头像要显示在子组件uploading
        // this.$refs.Uploading.imageUrl = this.userInfo.staffPhoto
      } catch (err) {
        console.log('', err)
      }
    },
    hSave() {
      // 手动表单兜底校验
      this.$refs.userForm.validate(valid => {
        if (valid) {
          this.doEdit()
        }
      })
    },
    async doEdit() {
      try {
        // 更新头像地址
        // this.userInfo.staffPhoto = this.$refs.Uploading.imageUrl
        const res = await saveUserDetailById(this.userInfo)
        console.log(res)
        this.$message.success('修改成功')
      // 保存数据
      } catch (err) {
        console.log(err)
        this.$message.error('修改失败')
      }
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>

